package ink.xiaobaibai.serviceOxy.onePay.impl;

import ink.xiaobaibai.serviceOxy.onePay.IDelayCloseService;
import ink.xiaobaibai.serviceOxy.onePay.IDeviceOzoneControlService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.ApplicationArguments;
import org.springframework.boot.ApplicationRunner;
import org.springframework.stereotype.Service;

/**
 * @description: 单次延时队列
 * @author: 小白白
 * @create: 2021-05-24
 **/

@Service
public class DelayCloseServiceImpl implements IDelayCloseService, ApplicationRunner {

    @Autowired
    private IDeviceOzoneControlService deviceControlService;

    /**
     * 点击源码查看,可以看到是线程安全的延时队列
     */
//    public MyDelayQueue<DeviceDelayItem> delayQueue=new MyDelayQueue<>();

//    @Override
//    public void add(Integer deviceId, Integer second,String orderIdOrMsg) {
//        DeviceDelayItemAddMsg item = new DeviceDelayItemAddMsg(orderIdOrMsg,deviceId, DateUtils.addSeconds(new Date(), second));
//        this.delayQueue.add(item);
//    }
//
//    @Override
//    public void remove(Integer deviceId) {
//        this.delayQueue.remove(new DeviceDelayItem(deviceId,null));
//    }
//
//    @Override
//    public boolean containByDeviceId(Integer deviceId){
//        return this.delayQueue.set.contains(deviceId);
//    }
    @Override
    public void run(ApplicationArguments args) throws Exception {
//        this.taskHandler();
    }

    private void taskHandler() {
//        ExecutorService executorService = Executors.newSingleThreadExecutor();
//        executorService.execute(()->{
//            while (true){
//                try {
//                    DeviceDelayItemAddMsg deviceDelayItem = (DeviceDelayItemAddMsg) delayQueue.take();
//                    Integer deviceId=deviceDelayItem.getDeviceId();
//                    String orderOrMsg=deviceDelayItem.getOrderOrMsg();
//                    deviceControlService.closeDeviceOnePay(deviceId,orderOrMsg);
//                } catch (InterruptedException e) {
//                    e.printStackTrace();
//                }
//            }
//        });
    }

}
